import java.util.Arrays;
import java.util.Scanner;

public class NguessNumber {
    int[] s = new int[4];
    int[] guess = new int[4];
    int[] sub = new int[]{0, 0};
    Scanner t = new Scanner(System.in);
    public int[] generateAnswer(){
        sub = new int[]{0, 0};
        boolean b = false;
        for (int i = 0; i<4;i++){
            s[i] = (int)(Math.random() * 10);
            for (int j = i-1;j>=0;j--){
                if (s[i]==s[j]){
                    b=true;
                    break;
                }
            }
            if (b){
                b=false;
                i--;
            }
        }
        return s;
    }
    public int[] getPlayerInput(){
        for (int i = 0;i<4;i++){
            guess[i] = t.nextInt();
        }
        return guess;
    }
    public String compareGuessAnswer(){
        sub = new int[]{0, 0};
        for (int i =0;i<4;i++){
            for (int j=0;j<4;j++){
                if (guess[j]>9||guess[j]<0){
                    sub[0]=-1;
                    return "请输入0-9的整数而不是"+guess[j];
                }
                if (s[i]==guess[j]){
                        if (i==j){
                            sub[0]++;
                        }else {
                            sub[1]++;
                        }
                }
            }
        }
        return sub[0]+"A"+sub[1]+"B";
    }
    public boolean isWin(){
        if (sub[0]==4){
            return true;
        }
            return false;
    }
    public static void main(String[] args) {
        int roundt;
        Scanner f = new Scanner(System.in);
        String cntnui="";
        NguessNumber s = new NguessNumber();
        while (!cntnui.equals("n")){
            int[] ansser = s.generateAnswer();
            int[] guess;
            roundt = 7;
            while (true){
                System.out.println("你还有"+roundt+"次机会");
                guess = s.getPlayerInput();
                System.out.println(s.compareGuessAnswer());
                if (s.compareGuessAnswer().length()>4){
                    roundt++;
                }
                roundt --;

                if (s.isWin()){
                System.out.println("猜对啦");
                break;
                }
                if (7-roundt==7){
                    System.out.println("你没有机会啦");
                    break;
                }
            }

            System.out.println("是否再次游玩y/n");
            cntnui = f.next();
        }
        f.close();
    }
}
